News = function(){
	return {
		
		initDataGrid : function() {
			$('#newsDataGrid').datagrid({
				url				: basePath + '/pc/news/list.htm',
				fit				: true,
				fitColumns		: true,
				striped			: true,
				rownumbers		: true,
				autoRowHeight	: true,
				nowrap			: true,
				pagination		: true,
				pageSize        : 20,
				pageList        : [10, 20, 30, 40, 50],
				method 			: 'POST',
				columns			: [[
				    {field : 'newsId',          title : 'newsId',    width : 100,   checkbox  : true},
				    {field : 'title',           title : '标题',      width : 180,   formatter : function(value, rowData, index) {
				    	if(value.length > 25) {
				    		value = value.substring(0, 25) + '...';
				    	}
		    			return '<a href="javascript:void(0)" onclick="News.showNewDetail(\''+rowData.newsId+'\')">'+value+'</a>';
				    }},
				    {field : 'publishMan',      title : '发布人',     width : 100,  align : 'center'},
				    {field : 'publishTime',     title : '发布时间',   width : 100,  align : 'center'},
				    {field : 'state',           title : '是否草稿',   width : 60,   align : 'center',   formatter : function(value, rowData, index) {
				    	var result = "";
				    	if(value == '0') {
				    		result = "是";
				    	} else {
				    		result = "否";
				    	}
				    	return result;
				    }},
				    {field : 'opt',           title : '操作',        width : 60,   align : 'center',   formatter : function(value, rowData, index) {
				    	var result = "";
				    	if(rowData.state == '0') {
				    		result = '<a href="javascript:void(0)" onclick="News.publish(\''+rowData.newsId+'\')">发布</a>';
				    	}
				    	return result;
				    }}
				]],
				toolbar     : [{
					text    : '添加',
					iconCls : 'add',
					handler : function() {
						News.addNews();
					}
				}, {
					text    : '修改',
					iconCls : 'edit',
					handler : function() {
						News.editNews();
					}
				}, {
					text    : '删除',
					iconCls : 'remove',
					handler : function() {
						News.removeNews();
					}
				}, {
					text    : '刷新',
					iconCls : 'reload',
					handler : function() {
						News.reload();
					}
				}]
			});
		},
		
		reload : function() {
			$('#newsDataGrid').datagrid('reload');
		},
		
		showNewDetail : function(newsId) {
			Ict.openWin('通知公告信息', 820, 420, "/pc/news/detail.htm?newsId=" + newsId);
		},
		
		publish : function(newsId) {
			Ict.confirm("确认发布吗?", function(yes) {
				if(yes) {
					$.post('/pc/news/publish.htm', {'newsId' : newsId}, function(data) {
						if(data.code == '0') {
							Ict.info("发布成功");
							$('#newsDataGrid').datagrid('reload');
						} else {
							Ict.error("发布失败");
						}
					});
				}
			});
		},
		
		addNews : function() {
			Ict.openWin2('发布新闻', 900, 500, "/pc/news/add.htm");
		},
		
		editNews : function() {
			var rows = $('#newsDataGrid').datagrid('getSelections');
			if(rows.length == 1) {
				Ict.openWin2('新闻', 900, 500, "/pc/news/edit.htm?newsId=" + rows[0].newsId);
			} else {
				Ict.warn("请选择一条记录!");
				return;
			}
		},
		
		removeNews : function() {
			var rows = $('#newsDataGrid').datagrid('getSelections');
			if(!rows || 0 == rows.length) {
				Ict.warn("请选择一条记录!");
   				return;
			}
			var idArray = "";
			for(var i = 0; i < rows.length; i++) {
				idArray += rows[i].newsId + ",";
			}
			idArray = idArray.substring(0, idArray.length - 1);
			Ict.confirm("是否删除已选?", function(yes) {
				if(yes) {
					$.post('/pc/news/remove.htm', {'newsIds' : idArray}, function(data) {
						if(data.code == '0') {
							Ict.info("删除成功");
							$('#newsDataGrid').datagrid('reload');
						} else {
							Ict.error("删除失败");
						}
					});
				}
			});
		},
		
		addSearchBox : function(width) {
			$('.searchbox').css({'width': width+'px'});
			$('.searchbox-text').css({'width': eval(width-20)+'px'});
			$('.datagrid-toolbar').append($('.serarchbox'));
			$('.datagrid-toolbar').append($('#tbar'));
		},
		
		doSearch : function(value) {
			$('#newsDataGrid').datagrid({
				queryParams   : {
					'keyword' : $.trim(value)
				},
				pageNumber    : 1,
				toolbar       : '#toolbar'
			});
		},
		
		init : function() {
			News.initDataGrid();
			News.addSearchBox(250);
		}
	};
}();

$(function(){
	News.init();
});